package org.jgs2007.onemall.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jgs2007.onemall.annotation.LoginInterceptorIgnore;
import org.jgs2007.onemall.entity.Admin;
import org.jgs2007.onemall.result.JsonResult;
import org.jgs2007.onemall.service.AdminService;
import org.jgs2007.onemall.util.MD5Utils;
import org.jgs2007.onemall.util.RedisUtils;
import org.jgs2007.onemall.util.ResponseUtil;
import org.jgs2007.onemall.vo.RegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private MD5Utils md5Utils;

    @Autowired
    private RedisUtils redisUtils;

    @RequestMapping("regist")
    @LoginInterceptorIgnore
    public Object regist(RegistVo registVo){

        // 判断用户名是否已注册
        Admin admin = adminService.getOne(Admin.builder().username(registVo.getUsername()).build());
        if(admin == null){
            // 密码进行md5加密
            try {
                registVo.setPassword(md5Utils.encode(registVo.getPassword()));
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
            // 保存用户注册信息
            adminService.save(Admin.builder().username(registVo.getUsername()).password(registVo.getPassword()).addTime(new Date()).build());
            return JsonResult.builder().code(200).msg("注册成功！").build();
        } else {
            throw new RuntimeException("用户名已被注册！");
        }
    }

    @PostMapping("login")
    // 不拦截的请求
    @LoginInterceptorIgnore
    public Object login(@RequestBody RegistVo registVo){

        // 密码进行md5加密
        try {
            registVo.setPassword(md5Utils.encode(registVo.getPassword()));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }

        // 根据用户名查询数据库
        Admin admin = adminService.getOne(Admin.builder().username(registVo.getUsername()).build());
        if(admin == null){
            throw new RuntimeException("用户名不存在");
        } else {
            // 根据用户名和密码查询数据库
            Admin admin1 = adminService.getOne(Admin.builder().username(registVo.getUsername()).password(registVo.getPassword()).build());
            if(admin1 == null){
                throw new RuntimeException("密码错误");
            }
            // 生成token
            String token = UUID.randomUUID().toString();
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                String adminJson = objectMapper.writeValueAsString(admin1);
                // 将token保存到redis数据库
                redisUtils.setValue("token", adminJson, 30, TimeUnit.MINUTES);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e.getMessage());
            }

            // 返回json格式的结果
            // userInfo
            Map<String, Object> adminInfo = new HashMap<>();
            adminInfo.put("nickName", "nickName");
            adminInfo.put("avatar", "5_pale2.jpg");

            Map<Object, Object> result = new HashMap<>();
            result.put("token", token);
            result.put("adminInfo", adminInfo);

            return ResponseUtil.ok(result);
        }
    }

    @GetMapping("info")
    public Object info() {

        Map<String, Object> data = new HashMap<>();
        data.put("name", "name");
        data.put("avatar", "5_pale2.jpg");
        Collection<String> apis = new HashSet<>();
        apis.add("*");
        data.put("perms", apis);
        Set<String> roles = new HashSet<>();
        roles.add("超六管理员");
        data.put("roles", roles);
        return ResponseUtil.ok(data);
    }

    @PostMapping("logout")
    public Object logout() {

        return ResponseUtil.ok();
    }
}
